{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "Test_trans = pd.read_csv('./test_operation_round2.csv')\n",
    "\n",
    "Test_trans['device2'] = Test_trans['device2'].str.strip()\n",
    "Test_trans['device1'] = Test_trans['device1'].str.strip()\n",
    "Test_trans['device_'] = Test_trans['device1']+'_'+Test_trans['device2']\n",
    "dev = Test_trans['device_'].value_counts()\n",
    "dev = dev[dev>4].index\n",
    "uuuid = []\n",
    "\n",
    "for i,d in enumerate(dev):\n",
    "    print(i/len(dev))\n",
    "    MI_6 = Test_trans[Test_trans['device_']==d]\n",
    "    nn = MI_6.sort_values('geo_code').drop_duplicates(['geo_code','UID'])#.set_index(['device1','geo_code'])\n",
    "    geo = nn['geo_code'].value_counts()\n",
    "    geo = geo[geo>4].index\n",
    "    aa = MI_6[MI_6['geo_code'].isin(geo)].groupby(['geo_code','day','mode','wifi'])['UID']\n",
    "    hh = aa.nunique()\n",
    "    en = hh[(hh>4)].reset_index()\n",
    "\n",
    "    for i in range(en.shape[0]):\n",
    "        bb = MI_6[(MI_6['wifi']== en.loc[i]['wifi'] )&(MI_6['geo_code']== en.loc[i]['geo_code'] )&(MI_6['day']==en.loc[i]['day'])&(MI_6['mode']==en.loc[i]['mode'])][['UID','day','time','device1','device2','geo_code']]\n",
    "        uuuid = uuuid + bb['UID'].value_counts().index.tolist()\n",
    "\n",
    "b = list(set(uuuid))\n",
    "print(len(b))\n",
    "#base38_1599_opera_wifi最高分：0.54564\n",
    "#9的时候，479， 合并后1589； 8的时候555，合并还是1589；7的时候626，合并1599；6的时候，626，合并仍然1599；5 的时候721，合并1626；4的时候758，合并1636"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "Test_trans = pd.read_csv('./test_transaction_round2.csv')\n",
    "Test_trans['device2'] = Test_trans['device2'].str.strip()\n",
    "Test_trans['device1'] = Test_trans['device1'].str.strip()\n",
    "Test_trans['device_'] = Test_trans['device1']+'_'+Test_trans['device2']\n",
    "dev = Test_trans['device_'].value_counts()\n",
    "\n",
    "dev = dev[dev>4].index\n",
    "uuuid = []\n",
    "\n",
    "for d in dev:\n",
    "    MI_6 = Test_trans[Test_trans['device_']==d]\n",
    "    nn = MI_6.sort_values('geo_code').drop_duplicates(['geo_code','UID'])#.set_index(['device1','geo_code'])\n",
    "    geo = nn['geo_code'].value_counts()\n",
    "    geo = geo[geo>4].index\n",
    "    hh = MI_6[MI_6['geo_code'].isin(geo)].groupby(['geo_code','day','merchant'])['UID'].nunique()\n",
    "    en = hh[hh>4].reset_index()\n",
    "\n",
    "    for i in range(en.shape[0]):\n",
    "        bb = MI_6[(MI_6['geo_code']== en.loc[i]['geo_code'] )&(MI_6['day']==en.loc[i]['day'])&(MI_6['merchant']==en.loc[i]['merchant'])][['UID','channel','day','time','device1','device2','geo_code','merchant']]\n",
    "        uuuid = uuuid + bb['UID'].value_counts().index.tolist()\n",
    "\n",
    "bb_ = list(set(uuuid))\n",
    "print(len(bb_))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "bbb = list(set(bb_).union(set(b)))\n",
    "print(len(bbb))\n",
    "fi_uid = pd.DataFrame(bbb,columns = ['UID'])\n",
    "fi_uid.head()#1362"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "Test_trans = pd.read_csv('./test_transaction_round2.csv')\n",
    "Test_tag = pd.read_csv('./lgb_baseline_12-12-22-05.csv')  # 测试样本\n",
    "\n",
    "test_rule_uid = pd.DataFrame(fi_uid.UID.unique())\n",
    "pred_data_rule = Test_tag.merge(test_rule_uid,left_on ='UID',right_on =0, how ='left')\n",
    "pred_data_rule['Tag'][(pred_data_rule[0]>0)] =1 # 这个系数还需要调整\n",
    "pred_data_rule[['UID', 'Tag']].to_csv('result.csv', index=False)\n",
    "a = pred_data_rule[pred_data_rule['Tag'] == 1]['UID'].values\n",
    "len(a)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
